//
//  Common.m
//  TeamTiger
//
//  Created by xxcao on 16/7/28.
//  Copyright © 2016年 MobileArtisan. All rights reserved.
//

#import "Common.h"
#import "HYBHelperBlocksKit.h"
#import "JKEncrypt.h"

@implementation Common



//去除UITableView多余分割线
+ (void)removeExtraCellLines:(UITableView *)tableView {
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    view.opaque = NO;
    [tableView setTableFooterView:view];
}

//字符串为空检查
+ (BOOL)isEmptyString:(NSString *)sourceStr {
    if ((NSNull *)sourceStr == [NSNull null]) {
        return YES;
    }
    if (sourceStr == nil) {
        return YES;
    }
    if (sourceStr == NULL) {
        return YES;
    }
    if ([sourceStr isEqual:[NSNull null]]) {
        return YES;
    }
    if (![sourceStr isKindOfClass:[NSString class]]) {
        return YES;
    }
    if([sourceStr isEqualToString:@"<null>"]){
        return YES;
    }
    if ([sourceStr isEqualToString:@"null"]) {
        return YES;
    }
    if ([sourceStr isEqualToString:@""]) {
        return YES;
    }
    if (sourceStr.length == 0) {
        return YES;
    }
    return NO;
}

//数组是否为空
+ (BOOL)isEmptyArr:(NSArray *)arr {
    if ( arr != nil && ![arr isKindOfClass:[NSNull class]] && arr.count != 0) {
        return NO;
    }
    return YES;
}

//获取安全的字符串
+ (NSString *)safeString:(NSString *)sourceStr {
    if([Common isEmptyString:sourceStr]){
        return @"";
    }
    return sourceStr;
}


//自定义push动画
+ (void)customPushAnimationFromNavigation:(UINavigationController *)nav ToViewController:(UIViewController *)vc Type:(NSString *)animationType SubType:(NSString *)subType{
    CATransition *transition = [CATransition animation];
    transition.duration = 0.3f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = animationType;
    transition.subtype = subType;
    [nav.view.layer addAnimation:transition forKey:nil];
    [nav pushViewController:vc animated:NO];
}

//自定义pop动画
+ (void)customPopAnimationFromNavigation:(UINavigationController *)nav Type:(NSString *)animationType SubType:(NSString *)subType{
    CATransition *transition = [CATransition animation];
    transition.duration = 0.3f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = animationType;
    transition.subtype = subType;
    [nav.view.layer addAnimation:transition forKey:nil];
    [nav popViewControllerAnimated:NO];
}

/*
 animation.type = kCATransitionFade;
 animation.type = kCATransitionPush;
 animation.type = kCATransitionReveal;
 animation.type = kCATransitionMoveIn;
 animation.type = @"cube";
 animation.type = @"suckEffect";
 animation.type = @"oglFlip";
 animation.type = @"rippleEffect";
 animation.type = @"pageCurl";
 animation.type = @"pageUnCurl";
 animation.type = @"cameraIrisHollowOpen";
 animation.type = @"cameraIrisHollowClose";
 
 
 CA_EXTERN NSString * const kCATransitionFromRight
 __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
 CA_EXTERN NSString * const kCATransitionFromLeft
 __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
 CA_EXTERN NSString * const kCATransitionFromTop
 __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
 CA_EXTERN NSString * const kCATransitionFromBottom
 __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
 
 */

//根据16进制显示颜色
+ (UIColor *)colorFromHexRGB:(NSString *)inColorString {
    UIColor *result = nil;
    unsigned int colorCode = 0;
    unsigned char redByte, greenByte, blueByte;
    if (![Common isEmptyString:inColorString]) {
        NSScanner *scanner = [NSScanner scannerWithString:inColorString];
        (void) [scanner scanHexInt:&colorCode]; // ignore error
    }
    redByte = (unsigned char) (colorCode >> 16);
    greenByte = (unsigned char) (colorCode >> 8);
    blueByte = (unsigned char) (colorCode); // masks off high bits
    result = [UIColor
              colorWithRed: (float)redByte / 0xff
              green: (float)greenByte/ 0xff
              blue: (float)blueByte / 0xff
              alpha: 1.0];
    return result;
}

//处理微信头像 头像大小 46 64 96 132
+ (NSString *)handleWeChatHeadImageUrl:(NSString *)headImgUrl Size:(double)size {
    if (![Common isEmptyString:headImgUrl]) {
        NSString *urlString = headImgUrl;
        NSMutableString *mString = [NSMutableString string];
        if ([urlString containsString:@".jpg"] || [urlString containsString:@".png"]) {
            mString = urlString.mutableCopy;
        } else {
            NSArray *components = [urlString componentsSeparatedByString:@"/"];
            NSInteger count = components.count;
            [components enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (idx != count - 1) {
                    [mString appendFormat:@"%@/",obj];
                } else {
                    [mString appendFormat:@"%d",(int)size];//头像大小 46 64 96 132
                }
            }];
        }
        return mString;
    }
    return nil;
}


//对字符串进行解密 返回字典 mode==0 十六进制模式   mode==1 字符串模式
+ (NSDictionary *)unEncypt2StrWithString:(NSString *)srcStr Mode:(NSInteger) mode{
    NSArray *array = [srcStr componentsSeparatedByString:@"&"];
    NSMutableDictionary *mDic = [NSMutableDictionary dictionary];
    JKEncrypt *jkEncrypt = [[JKEncrypt alloc] init];
    [array enumerateObjectsUsingBlock:^(NSString *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSArray *keyValues = [obj componentsSeparatedByString:@"="];
        if (keyValues.count == 2) {
            if (mode == 0) {
                mDic[keyValues[0]] = [jkEncrypt doDecEncryptHex:keyValues[1]];
            } else {
                mDic[keyValues[0]] = [jkEncrypt doDecEncryptStr:keyValues[1]];
            }
        }
    }];
    return mDic;
}

//对字典进行解密 返回字典  mode==0 十六进制模式   mode==1 字符串模式
+ (NSDictionary *)unEncypt2DicWithDic:(NSDictionary *)srcDic Mode:(NSInteger)mode{
    NSMutableDictionary *mDic = [NSMutableDictionary dictionary];
    JKEncrypt *jkEncrypt = [[JKEncrypt alloc] init];
    [srcDic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        if (mode == 0) {
            mDic[key] = [jkEncrypt doDecEncryptHex:obj];
        } else {
            mDic[key] = [jkEncrypt doDecEncryptStr:obj];
        }
    }];
    return mDic;
}

//对字典加密 返回字典 key=value&key1=value2  mode==0 十六进制模式   mode==1 字符串模式
+ (NSDictionary *)encypt2DicWithDictionary:(NSDictionary *)srcDic UnencyptKeys:(NSArray *)keys Mode:(NSInteger)mode{
    NSMutableDictionary *mDic = [NSMutableDictionary dictionary];
    JKEncrypt *jkEncrypt = [[JKEncrypt alloc] init];
    [srcDic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, NSString * _Nonnull obj, BOOL * _Nonnull stop) {
        NSString *tmpStr = nil;
        if ([keys containsObject:key]) {
            tmpStr = [NSString stringWithFormat:@"%@=%@", key, obj];
        } else {
            if (mode == 0) {
                tmpStr = [NSString stringWithFormat:@"%@=%@", key, [jkEncrypt doEncryptHex:obj]];
            } else {
                tmpStr = [NSString stringWithFormat:@"%@=%@", key, [jkEncrypt doEncryptStr:obj]];
            }
        }
        mDic[key] = tmpStr;
    }];
    return mDic;
}




@end
